﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
using System.IO;
using LIUS.Beta.FileSystem.PathFileSuffix;

namespace Kopain2
{

    /// <summary>This class handles the Most Recently Used list.
    /// </summary>
    class MRUList : List<MRUItem>
    {

        /// <summary>Default constructor made private so as to hinder the usage externally.
        /// </summary>
        private MRUList() { }

        /// <summary>This constructor populates the MRU list with what is in the user settings.
        /// </summary>
        internal static MRUList Populate()
        {
            MRUList ret = new MRUList();

            // Retrieve the user settings.
            XmlDocument persistedMRUListDocument = Properties.Settings.Default.MRUList;

            if (null == persistedMRUListDocument)
            {
                // We ain't got anything so don't do anything either.
            }
            else
            {
                // Deserialize.
                // http://geekswithblogs.net/timh/archive/2006/02/09/68857.aspx
                XmlNodeReader nodeReader = new XmlNodeReader(persistedMRUListDocument.DocumentElement);
                XmlSerializer serializer = new XmlSerializer(typeof(Schemas.MRUList));
                Schemas.MRUList mruList = (Schemas.MRUList)serializer.Deserialize(nodeReader);

                // Copy data into this class.
                if (null != mruList.Concept)
                {
                    for (int i = 0; i < mruList.Concept.Length; i++)
                    {
                        //TODO:Validate uniqueness debugtime.
                        ret.Add(new MRUItem(mruList.Concept[i].Title, new PathFilenameSuffix( mruList.Concept[i].PathFileSuffix, HandleValue.RequireExact)));
                        //PathAndFileList.Add(mruList.File[i].PathAndFile);
                    }
                }
                else
                {
                    //TODO:Set something that is a disbled text like "There is no recent document".
                }
            }

            return ret;
        }

        /// <summary>This method persists the data in the user settings.
        /// </summary>
        internal void Save()
        {
            // Copy data from this class into the serializable class.
            Schemas.MRUList mruList = new Schemas.MRUList();
            mruList.Concept = new Schemas.Concept[this.Count];
            for (int i = 0; i < this.Count; i++)
            {
                mruList.Concept[i] = new Schemas.Concept(this[i].Title, this[i].PathFilenameSuffix);
            }

            // Serialize.
            // http://geekswithblogs.net/timh/archive/2006/02/09/68857.aspx
            XmlSerializer serializer = new XmlSerializer(typeof(Schemas.MRUList ) );
            StringBuilder stringBuilder = new StringBuilder();
            StringWriter stringWriter = new StringWriter(stringBuilder);
            serializer.Serialize(stringWriter, mruList);
            XmlDocument document = new XmlDocument();
            document.LoadXml(stringBuilder.ToString() );
         
            // Persist.
            Properties.Settings.Default.MRUList = document;
            Properties.Settings.Default.Save();
        }

    }

}
